/*
 * Copyright 2004-2006 公司名
 * 本软件及其所有拷贝的名称，与光盘上或本软件中注明的公司同在。
 * 本软件及文档享有版权，并受国家版权法及国际协约条款的保护。
 * 您不可以从本软件中去掉其版权声明；并保证为本软件的拷贝（本部或部分）复制版权声明。
 * 您同意制止以任何形式非法拷贝本软件及文档。
 * 版权信息参见：
 *      http://www.companyname.cn/licenses
 */

package com.companyname.common.sysframe.dao;

import java.util.List;

import org.apache.log4j.Logger;

import com.companyname.common.base.dao.BaseDAOHibernate;
import com.companyname.common.sysframe.define.UserStatus;
import com.companyname.common.sysframe.model.User;
import com.companyname.common.sysframe.util.CompanyNameSysFrameException;

/**
 * <p>Title: 人员</p>
 * <p>Description: 人员 User </p>
 * <p>Copyright: Copyright (c) 2004-2006</p>
 * <p>Company: 公司名</p>
 * @ $Author: 作者名 $
 * @ $Date: 创建日期 $
 * @ $Revision: 1.0 $
 * @ created in 创建日期
 *
 */

public class UserDAOimp extends BaseDAOHibernate
                        implements UserDAO
{
        static Logger logger = Logger.getLogger(UserDAOimp.class);

        /**
         * 描述:人员
         *
         * @ param:
         * @ param:orderBy 排序属性（for hql）
         * @ param:sortType 排序方式（asc or desc）
         * @ Exception:
         * @ return model集合
         */
        public List findUsers(String orgId,
                              String roleId,
                              String postId,
                              String account,
                              String name,
                              String address,
                              String email,
                              String phone,
                              String homeSite,
                              String mobile,
                              String status,
                              String orderBy,
                              String sortType)

        {
                if(!orgId.equals("%"))
                {
                        String hql = 
                        " select distinct user from User user,Post2User p2u,Org org " + 
                        " where( user.account like ? " + 
                        " and (user.familyName + user.givenName) like ? " +
                        " and user.address like ? " + 
                        " and user.email like ? " + 
                        " and user.phone like ? " + 
                        " and user.homeSite like ? " + 
                        " and user.mobile like ? " + 
                        " and user.status like '" + status + "' " +
                        " and user.status != ? " + 
                        " and user = p2u.user  " +
                        " and p2u.post.org.id = ? " +
                        " ) " + 
                        " order by " + orderBy + " " + sortType;
                        Object[] values = new Object[9];
                        values[0] = "%" + account + "%";
                        values[1] = "%" + name + "%";
                        values[2] = "%" + address + "%";
                        values[3] = "%" + email + "%";
                        values[4] = "%" + phone + "%";
                        values[5] = "%" + homeSite + "%";
                        values[6] = "%" + mobile + "%";
                        values[7] = new Integer(UserStatus.BLANK_OUT);
                        values[8] = orgId;
                        return this.getHibernateTemplate().find(hql,values);
                }
                if(!roleId.equals("%"))
                {
                        String hql = 
                        " select distinct user from User user,Post2User p2u,Role role " + 
                        " where( user.account like ? " + 
                        " and (user.familyName + user.givenName) like ? " +
                        " and user.address like ? " + 
                        " and user.email like ? " + 
                        " and user.phone like ? " + 
                        " and user.homeSite like ? " + 
                        " and user.mobile like ? " + 
                        " and user.status like '" + status + "' " +
                        " and user.status != ? " + 
                        " and user = p2u.user  " +
                        " and p2u.post.role.id = ? " +
                        " ) " + 
                        " order by " + orderBy + " " + sortType;
                        Object[] values = new Object[9];
                        values[0] = "%" + account + "%";
                        values[1] = "%" + name + "%";
                        values[2] = "%" + address + "%";
                        values[3] = "%" + email + "%";
                        values[4] = "%" + phone + "%";
                        values[5] = "%" + homeSite + "%";
                        values[6] = "%" + mobile + "%";
                        values[7] = new Integer(UserStatus.BLANK_OUT);
                        values[8] = roleId;
                        return this.getHibernateTemplate().find(hql,values);
                }
                
                if(!postId.equals("%"))
                {
                        String hql = 
                        " select distinct user from User user,Post2User p2u " + 
                        " where( user.account like ? " + 
                        " and (user.familyName + user.givenName) like ? " +
                        " and user.address like ? " + 
                        " and user.email like ? " + 
                        " and user.phone like ? " + 
                        " and user.homeSite like ? " + 
                        " and user.mobile like ? " + 
                        " and user.status like '" + status + "' " +
                        " and user.status != ? " + 
                        " and user = p2u.user  " +
                        " and p2u.post.id = ? " +
                        " ) " + 
                        " order by " + orderBy + " " + sortType;
                        Object[] values = new Object[9];
                        values[0] = "%" + account + "%";
                        values[1] = "%" + name + "%";
                        values[2] = "%" + address + "%";
                        values[3] = "%" + email + "%";
                        values[4] = "%" + phone + "%";
                        values[5] = "%" + homeSite + "%";
                        values[6] = "%" + mobile + "%";
                        values[7] = new Integer(UserStatus.BLANK_OUT);
                        values[8] = postId;
                        return this.getHibernateTemplate().find(hql,values);
                }
                
                String hql = 
                        " select user from User user" + 
                        " where( user.account like ? " + 
                        " and (user.familyName + user.givenName) like ? " +
                        " and user.address like ? " + 
                        " and user.email like ? " + 
                        " and user.phone like ? " + 
                        " and user.homeSite like ? " + 
                        " and user.mobile like ? " + 
                        " and user.status like '" + status + "' " +
                        " and user.status != ? " + 
                        " ) " + 
                        " order by " + orderBy + " " + sortType;
                        Object[] values = new Object[8];
                        values[0] = "%" + account + "%";
                        values[1] = "%" + name + "%";
                        values[2] = "%" + address + "%";
                        values[3] = "%" + email + "%";
                        values[4] = "%" + phone + "%";
                        values[5] = "%" + homeSite + "%";
                        values[6] = "%" + mobile + "%";
                        values[7] = new Integer(UserStatus.BLANK_OUT);
                        return this.getHibernateTemplate().find(hql,values);
        }

        public User getByAccount(String account)
        {
                String hql = 
                        " from User " +
                        " where( account = ? " +
                        " and status != ? ) ";
                Object[] values = new Object[2];
                values[0] = account;
                values[1] = UserStatus.BLANK_OUT;
                List users =  this.getHibernateTemplate().find(hql,values);
                if(users.size() == 0)
                {
                        return null;
                }
                if(users.size() > 1)
                {
                        throw new CompanyNameSysFrameException("帐号为" + account + "的人员有" + users.size() + "个");
                }
                return (User)users.get(0);
        }
}

